package com.weilive.api.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.weilive.api.ServerConfig;
import com.weilive.api.ServerErrors;
import com.weilive.api.dao.mapper.ReportUserMapper;
import com.weilive.api.dao.mapper.UserMapper;
import com.weilive.api.dao.mapper.UserPunishRecordMapper;
import com.weilive.api.dao.model.ReportUser;
import com.weilive.api.dao.model.UserInfo;
import com.weilive.api.dao.model.UserPunishRecord;
import com.weilive.api.gate.json.protocol.AnchorInfoModel;
import com.weilive.api.gate.json.protocol.BaseInfoModel;
import com.weilive.api.gate.json.protocol.CSA_UpdateUserInfo;
import com.weilive.api.gate.json.protocol.FansModel;
import com.weilive.api.gate.json.protocol.GiftTopModel;
import com.weilive.api.gate.json.protocol.RoomInfoModel;
import com.weilive.api.gate.json.protocol.SysMsgModel;
import com.weilive.api.gate.json.protocol.VideoModel;
import com.weilive.api.utils.Ks3Util;
import com.weilive.api.utils.WcsUtil;
import com.weilive.core.cache.redis.Order;
import com.weilive.core.cache.redis.RedisConfig;
import com.weilive.core.cache.redis.RedisService;
import com.weilive.core.cache.redis.TupleHelp;
import com.weilive.core.database.DynamicDataSource;
import com.weilive.core.message.Message;
import com.weilive.core.message.MessageListener;
import com.weilive.core.message.MessageSystem;
import com.weilive.core.message.MessageType;
import com.weilive.core.netty.NettyException;
import com.weilive.core.utils.Base64Utils;
import com.weilive.core.utils.DateUtil;
import com.weilive.core.utils.Utils;

@Service
public class UserSystem {

	private static final Logger logger = LoggerFactory.getLogger(UserSystem.class);
	@Autowired
	private RedisService redis;
	@Autowired
	private UserMapper userDao;
	@Autowired
	private ReportUserMapper reportDao;
	@Autowired
	private UserPunishRecordMapper punishRecordDao;
	@Autowired
	private BlackSystem blackSystem;
	@Autowired
	private DataPool dataPool;

	@PostConstruct
	public void init() {
		// 注册异步事件
		MessageSystem.registerMessageListener(MessageType.DeleteVideo, new MessageListener() {

			@Override
			public boolean retryable() {
				return true;
			}

			@Override
			public void onMessage(Message message) {
				if (message.getType() != MessageType.DeleteVideo) {
					return;
				}
				DeleteVideo video = (DeleteVideo) message.getObject();
				// 删除视频
				WcsUtil.deleteVdoPrefix(WcsUtil.getFileKey(video.getVdoName()));
			}
		});

		redis.exists("weilive.key.api");
		logger.info("redis初始化成功!");
	}

	private static final class DeleteVideo {
		private String vdoName;

		public String getVdoName() {
			return vdoName;
		}

		public void setVdoName(String vdoName) {
			this.vdoName = vdoName;
		}
	}

	private int isVip(Date vipDate) {
		if (vipDate == null) {
			return 0;
		}
		if (vipDate.before(new Date())) {
			return 0;
		}
		return 1;
	}

	/**
	 * 获取薪酬排行
	 * 
	 * @param userId
	 * @param page
	 * @return
	 */
	public List<GiftTopModel> getGiftTop(long userId, int page) {
		if (page < 0) {
			page = 0;
		}
		int begin = page * ServerConfig.getPageMaxcount();
		int end = begin + ServerConfig.getPageMaxcount() - 1;

		List<GiftTopModel> rankList = new ArrayList<GiftTopModel>();

		if (userId > 0) {
			List<TupleHelp> userList = redis.zrangeWithScores(RedisConfig.TOP_GIFT_LIST + userId, begin, end,
					Order.DESC);
			// 查找用户
			for (TupleHelp tuple : userList) {

				UserInfo user = getUserInfo(Long.parseLong(tuple.getKey()));
				if (user != null) {
					GiftTopModel top = new GiftTopModel();
					top.setUserId(user.getUserId());
					top.setPrice((int) tuple.getValue());
					top.setUserAlias(user.getUserAlias());
					top.setUserPic(user.getUserPic());
					top.setVipLv(isVip(user.getVipExpiration()));// 是否vip,0否,1:是

					rankList.add(top);
				}

			}
		}
		logger.info("获取薪酬排行榜成功");
		return rankList;
	}

	/**
	 * 获取用户基本信息
	 * 
	 * @param userId
	 * @return
	 */
	public BaseInfoModel getBaseInfoModel(long userId) {
		BaseInfoModel model = new BaseInfoModel();

		if (userId > 0) {

			UserInfo user = getUserInfo(userId);

			RoomInfoModel roomInfo = dataPool.getRoomInfo(userId);

			if (roomInfo != null && user != null) {
				model.setUserInfo(roomInfo);
				model.setGotGiftNum(user.getGiftNum());
				model.setFansCount((int) redis.zscore(RedisConfig.Z_LIKES_COUNT, String.valueOf(userId)));
				model.setGuanzCount((int) redis.size(RedisConfig.USER_ME_FANS_PREF + userId));
				model.setVideoCount((int) redis.size(RedisConfig.VIDEO_LIST + userId));

				model.setVipLevel(isVip(user.getVipExpiration()));
			}
		}
		return model;
	}

	public List<SysMsgModel> getSysMsgList(long userId) {
		List<SysMsgModel> msgList = new ArrayList<SysMsgModel>();

		List<UserPunishRecord> records = punishRecordDao.getAllRecords(userId);
		for (UserPunishRecord record : records) {
			SysMsgModel model = new SysMsgModel();
			model.setCreatedTime(DateUtil.dateFormat(record.getCreateTime()));

			StringBuilder sb = new StringBuilder();
			sb.append("你于");
			sb.append(DateUtil.dateFormat(record.getCreateTime()));
			sb.append("因为涉及");
			sb.append(record.getPunishReason());
			sb.append("受到");
			sb.append(record.getContinueTimeName());
			sb.append(record.getPunishOperateName());
			sb.append("处罚!");
			if (record.getDecloseRoomDatetime() != null) {
				sb.append("解封日期:");
				sb.append(DateUtil.dateFormat(record.getDecloseRoomDatetime())).append(".");
			}
			model.setSysMsg(sb.toString());
			msgList.add(model);
		}

		return msgList;
	}

	int getOpenStatus(long userId, Integer userSystemId) {
		if (userSystemId == null) {
			return 0;
		}
		// ios
		if (userSystemId == 0) {
			return redis.exists(RedisConfig.USER_DEV_TOKEN_IOS, String.valueOf(userId)) ? 1 : 0;
		} else if (userSystemId == 1) {
			return redis.exists(RedisConfig.USER_DEV_TOKEN_ANDROID, String.valueOf(userId)) ? 1 : 0;
		}
		return 0;
	}

	public int updateOpenStatus(long userId, int cmdType) {

		UserInfo user = getUserInfo(userId);
		if (user == null) {
			return 0;
		}
		int userSystemId = user.getUserSystemId();
		// userSystemId 0:ios 1: android
		if (userSystemId == 0) {
			// 1:开启,2:关闭
			if (cmdType == 2) {
				// 删除
				redis.del(RedisConfig.USER_DEV_TOKEN_IOS, userId + "");
			} else if (cmdType == 1) {
				// 开启
				redis.sadd(RedisConfig.USER_DEV_TOKEN_IOS, userId + "");
			}
		} else if (userSystemId == 2) {
			if (cmdType == 1) {
				// 删除
				redis.del(RedisConfig.USER_DEV_TOKEN_ANDROID, userId + "");
			} else if (cmdType == 1) {
				// 开启
				redis.sadd(RedisConfig.USER_DEV_TOKEN_ANDROID, userId + "");
			}
		}
		return cmdType == 1 ? 1 : 0;
	}

	/**
	 * 获取主播基本信息
	 * 
	 * @param userId
	 * @return
	 */
	public AnchorInfoModel getAnchorInfoModel(long userId, long anchorId) {

		AnchorInfoModel model = new AnchorInfoModel();

		String fansKey = null;
		if (userId > 0) {
			fansKey = RedisConfig.USER_ME_FANS_PREF + userId;
		}
		// 获取用户信息
		UserInfo user = getUserInfo(anchorId);
		// 获取房间信息
		RoomInfoModel roomInfo = dataPool.getRoomInfo(anchorId);

		if (user != null && roomInfo != null) {
			model.setUserInfo(roomInfo);

			model.setIsFans(0);
			if (fansKey != null) {
				model.setIsFans(Utils.isFans(redis.exists(fansKey, String.valueOf(anchorId))));
			}
			model.setFansCount((int) redis.zscore(RedisConfig.Z_LIKES_COUNT, String.valueOf(anchorId)));

			model.setGotGiftNum(user.getGiftNum());
			model.setSystemMsg(user.getUserMsg());
		}

		return model;
	}

	/**
	 * 检测用户名
	 * 
	 * @param userAlias
	 * @return
	 */
	public boolean checkUserAlias(String userAlias) {
		// String oldUserAlias = userDao.getUserAlias(userAlias);
		// if (oldUserAlias != null) {
		// // 已存在的userAlias
		// throw new NettyException(ServerErrors.USER_ALIAS_EXISTS);
		// }
		if (redis.exists(RedisConfig.USER_ALIASES, userAlias.toLowerCase())) {
			// 已存在的userAlias
			throw new NettyException(ServerErrors.USER_ALIAS_EXISTS);
		}
		if (blackSystem.isIllegaName(userAlias)) {
			throw new NettyException(ServerErrors.USER_ALIAS_INVALID);
		}
		if (!Utils.checkCharset(userAlias)) {
			throw new NettyException(ServerErrors.USER_ALIAS_INVALID);
		}
		if (userAlias.length() > ServerConfig.getUserAliasLen()) {
			throw new NettyException(ServerErrors.USER_ALIAS_ERROR_LEN);
		}
		return true;
	}

	private boolean checkUserProfile(String userProfile) {

		if (!Utils.checkCharset(userProfile)) {
			throw new NettyException(ServerErrors.USER_PROFILE_INVALID);
		}
		if (userProfile.length() > ServerConfig.getUserProfileLen()) {
			throw new NettyException(ServerErrors.USER_PROFILE_ERROR_LEN);
		}

		return true;
	}

	/**
	 * 修改用户信息
	 * 
	 * @param userId
	 * @param profile
	 * @return
	 */
	@Transactional
	public CSA_UpdateUserInfo setUserInfo(long userId, String profile, String userAlias, String userPic) {

		CSA_UpdateUserInfo response = new CSA_UpdateUserInfo();

		UserInfo user = getUserInfo(userId);

		if (user != null) {
			String oldUserAlias = user.getUserAlias();
			String oldPicURL = user.getUserPic();
			String newPicURL = null;
			byte[] fileValue = null;
			// 可以为空字符串
			if (userAlias != null) {
				if (checkUserAlias(userAlias)) {
					user.setUserAlias(userAlias);
				}
			}
			// 可以为空字符串
			if (profile != null) {
				if (checkUserProfile(profile)) {
					// 简介过滤掉
					profile = blackSystem.filterIllegaWord(profile);
					user.setProfilePic(profile);
				}
			}
			// 不能为空串
			if (!StringUtils.isBlank(userPic)) {

				fileValue = Base64Utils.decoder(userPic);

				newPicURL = Ks3Util.getPhotoURL(Utils.randomUID());
				user.setUserPic(newPicURL);
			}
			// mysql 修改
			userDao.setUserInfo(userId, user.getProfilePic(), user.getUserAlias(), user.getUserPic());
			if (!oldUserAlias.equals(user.getUserAlias())) {
				// 删除老的
				redis.del(RedisConfig.USER_ALIASES, oldUserAlias.toLowerCase());
				// 加入到redis缓存
				redis.sadd(RedisConfig.USER_ALIASES, user.getUserAlias().toLowerCase());
			}
			if (newPicURL != null && fileValue != null) {
				// 修改成功
				Ks3Util.deleteFile(Ks3Util.getKs3PhotoKey(oldPicURL));
				Ks3Util.uploadFile(fileValue, Ks3Util.getKs3PhotoKey(newPicURL));
			}

			String userKey = RedisConfig.USER_INFO_PREF + userId;
			if (redis.exists(userKey)) {
				Map<String, String> setMap = new HashMap<String, String>();
				setMap.put(RedisConfig.USER_PROFILE_PIC, user.getProfilePic());
				setMap.put(RedisConfig.USER_ALIAS, user.getUserAlias());
				setMap.put(RedisConfig.USER_PIC, user.getUserPic());
				setMap.put(RedisConfig.USER_UPDATED_TIME, DateUtil.dateFormat(new Date()));

				redis.hmset(userKey, setMap);
			}
			String roomKey = RedisConfig.USER_ROOM_PREF + userId;
			if (redis.exists(roomKey)) {
				Map<String, String> setMap = new HashMap<String, String>();
				setMap.put(RedisConfig.USER_ROOME_PROFILE_PIC, user.getProfilePic());
				setMap.put(RedisConfig.USER_ROOME_USER_ALIAS, user.getUserAlias());
				setMap.put(RedisConfig.USER_ROOME_USER_PIC, user.getUserPic());

				redis.hmset(roomKey, setMap);
			}

			response.setUserAlias(user.getUserAlias());
			response.setUserProfile(user.getProfilePic());
			response.setUserPic(user.getUserPic());
		}

		return response;
	}

	/**
	 * 获取粉丝列表
	 * 
	 * @param userId
	 * @param page
	 * @return
	 */
	public List<FansModel> getFansModelList(long userId, int page) {
		if (page < 0) {
			page = 0;
		}
		int begin = page * ServerConfig.getPageMaxcount();
		int end = begin + ServerConfig.getPageMaxcount() - 1;

		List<FansModel> modelList = new ArrayList<FansModel>();
		String fansKey = RedisConfig.USER_ME_FANS_PREF + userId;

		if (userId > 0) {
			String key = RedisConfig.USER_FANS_ME_PREF + userId;

			List<String> userList = redis.list(key, begin, end);

			for (String fansUserId : userList) {

				RoomInfoModel roomInfo = dataPool.getRoomInfo(Utils.parseLong(fansUserId));

				if (roomInfo != null) {
					FansModel model = new FansModel();
					model.setUserInfo(roomInfo);
					model.setIsFans(Utils.isFans(redis.exists(fansKey, fansUserId)));

					modelList.add(model);
				} else {
					// 如果用户不存在,删除掉
					redis.del(key, fansUserId);
					// 粉丝数减1
					redis.zadd(RedisConfig.Z_LIKES_COUNT, String.valueOf(userId),
							Math.max(redis.zscore(RedisConfig.Z_LIKES_COUNT, String.valueOf(userId)) - 1, 0));
					logger.info("删除粉丝用户---userId=" + userId);
				}

			}
		}

		return modelList;
	}

	/**
	 * 获取关注列表
	 * 
	 * @param userId
	 * @param page
	 * @return
	 */
	public List<FansModel> getGuanzhuModelList(long userId, int page) {
		if (page < 0) {
			page = 0;
		}
		int begin = page * ServerConfig.getPageGuanzhuMaxcount();
		int end = begin + ServerConfig.getPageGuanzhuMaxcount() - 1;

		List<FansModel> modelList = new ArrayList<FansModel>();

		if (userId > 0) {

			String key = RedisConfig.USER_ME_FANS_PREF + userId;

			List<String> userList = redis.list(key, begin, end);

			for (String fansUserId : userList) {
				RoomInfoModel roomInfo = dataPool.getRoomInfo(Utils.parseLong(fansUserId));

				if (roomInfo != null) {
					FansModel model = new FansModel();
					model.setUserInfo(roomInfo);
					model.setIsFans(1);

					modelList.add(model);
				} else {
					redis.del(key, fansUserId);
					logger.info("删除关注用户---userId=" + userId);
				}
			}
		}
		return modelList;
	}

	/**
	 * 获取历史影片
	 * 
	 * @param userId
	 * @param page
	 * @return
	 */
	public List<VideoModel> getVideoModelList(long userId, int page) {
		if (page < 0) {
			page = 0;
		}
		// 从哪页开始
		int begin = page * ServerConfig.getPageMaxcount();
		int end = begin + ServerConfig.getPageMaxcount() - 1;

		List<VideoModel> videlList = new ArrayList<VideoModel>();
		if (userId > 0) {
			String key = RedisConfig.VIDEO_LIST + userId;

			List<String> videoList = redis.list(key, begin, end);
			for (String videoId : videoList) {
				Map<String, String> video = redis.hgetAll(videoId);

				if (!video.isEmpty()) {
					VideoModel model = new VideoModel();
					model.setCreatedTime(video.get(RedisConfig.VDO_CREATED_TIME));
					model.setPicName(video.get(RedisConfig.VDO_PIC_NAME));
					model.setTitle(video.get(RedisConfig.VDO_TITLE));
					model.setVdoId(video.get(RedisConfig.VDO_ID));
					model.setVdoName(video.get(RedisConfig.VDO_NAME));
					model.setKey(video.get(RedisConfig.VDO_KEY));
					model.setIsDeleted(video.get(RedisConfig.VOD_IS_DELETED));

					videlList.add(model);
				}
			}
		}
		return videlList;
	}

	/**
	 * 获取推荐影片
	 * 
	 * @param userId
	 * @param page
	 * @return
	 */
	public List<VideoModel> getVideoShowlList(long userId) {
		// 从哪页开始
		List<VideoModel> videlList = new ArrayList<VideoModel>();
		if (userId > 0) {

			List<String> videoIds = redis.zrange(RedisConfig.VIDEO_SHOW_LIST + userId, 0, -1, Order.DESC);

			for (String videoId : videoIds) {
				if (!StringUtils.isBlank(videoId)) {
					Map<String, String> video = redis.hgetAll(videoId);

					if (!video.isEmpty()) {
						VideoModel model = new VideoModel();
						model.setCreatedTime(video.get(RedisConfig.VDO_CREATED_TIME));
						model.setPicName(video.get(RedisConfig.VDO_PIC_NAME));
						model.setTitle(video.get(RedisConfig.VDO_TITLE));
						model.setVdoId(video.get(RedisConfig.VDO_ID));
						model.setVdoName(video.get(RedisConfig.VDO_NAME));
						model.setKey(video.get(RedisConfig.VDO_KEY));
						model.setIsDeleted(video.get(RedisConfig.VOD_IS_DELETED));

						videlList.add(model);
					}
				}
			}
		}
		return videlList;
	}

	/**
	 * 删除历史影片
	 * 
	 * @param userId
	 * @param keyId
	 */
	public void deleteVideo(long userId, String keyId) {

		if (userId > 0) {
			String videoKey = RedisConfig.VIDEO_ID + keyId;

			String key = RedisConfig.VIDEO_LIST + userId;

			List<String> params = new ArrayList<String>();
			params.add(RedisConfig.VDO_NAME);
			params.add(RedisConfig.VDO_PIC_NAME);

			List<String> query = redis.hmget(videoKey, params);

			if (query != null && query.size() == 2) {
				String vdoName = query.get(0);
				if (StringUtils.isBlank(vdoName)) {
					vdoName = query.get(1);
				}
				// mysql删除
				DynamicDataSource.setVideo();
				// 删除
				userDao.deleteVideo(Long.parseLong(keyId));
				// 删除list里面的内容
				redis.del(key, videoKey);
				// 删除map
				redis.del(videoKey);

				if (!StringUtils.isBlank(vdoName)) {
					DeleteVideo deleteMsg = new DeleteVideo();
					deleteMsg.setVdoName(vdoName);
					MessageSystem.createMessage(MessageType.DeleteVideo, deleteMsg);
				}
			}
		}
	}

	/**
	 * 举报用户
	 * 
	 * @param userId
	 * @param reportUserId
	 * @param reportType
	 */
	public void reportUser(long userId, long reportUserId, int reportType) {
		ReportUser user = reportDao.selectReportUser(userId, reportUserId);

		if (user == null) {
			user = new ReportUser();
			user.setCreatedTime(new Date());
			user.setUpdatedTime(new Date());
			user.setProcessStatus(1);
			user.setReportCategoryId(reportType);
			user.setFromUserId(userId);
			user.setReporterUserId(reportUserId);
			reportDao.insertSelective(user);

		} else {
			user.setReportCategoryId(reportType);
			user.setUpdatedTime(new Date());
			reportDao.updateByPrimaryKey(user);
		}
	}

	public UserInfo getUserInfo(long userId) {
		Map<String, String> userData = redis.hgetAll(RedisConfig.USER_INFO_PREF + userId);
		if (userData == null || userData.isEmpty()) {
			// 初始化
			userData = new HashMap<String, String>();

			UserInfo info = userDao.getUserInfo(userId);

			if (info == null) {
				return null;
			}

			userData.put(RedisConfig.USER_ID, Utils.parseString(info.getUserId(), "0"));
			userData.put(RedisConfig.USER_PASS, Utils.parseString(info.getUserPass(), ""));
			userData.put(RedisConfig.USER_ACCOUNT, Utils.parseString(info.getUserAccount(), ""));
			userData.put(RedisConfig.USER_ALIAS, Utils.parseString(info.getUserAlias(), ""));
			userData.put(RedisConfig.USER_TYPE_ID, Utils.parseString(info.getUserTypeId(), "0"));
			userData.put(RedisConfig.USER_PIC, Utils.parseString(info.getUserPic(), ""));
			userData.put(RedisConfig.USER_E_MONEY, Utils.parseString(info.geteMoney(), "0"));
			userData.put(RedisConfig.USER_E_MONEY_USED, Utils.parseString(info.geteMoneyUsed(), "0"));
			userData.put(RedisConfig.USER_GIFT_COIN, Utils.parseString(info.getGiftCoin(), "0"));
			userData.put(RedisConfig.USER_RED_CNT, Utils.parseString(info.getRedCnt(), "0"));
			userData.put(RedisConfig.USER_GOT_RED_CNT, Utils.parseString(info.getGotRedCnt(), "0"));
			userData.put(RedisConfig.USER_PROFILE_PIC, Utils.parseString(info.getProfilePic(), ""));
			userData.put(RedisConfig.USER_VIP_EXPIRATION, info.getVipExpiration() == null ? "0"
					: Utils.parseString((info.getVipExpiration().getTime() / 1000L), "0"));
			userData.put(RedisConfig.USER_VJ_TYPE_ID, Utils.parseString(info.getVjTypeId(), "0"));
			userData.put(RedisConfig.USER_VJ_SUBTYPE_ID, Utils.parseString(info.getVjSubtypeId(), "0"));
			userData.put(RedisConfig.USER_ROOM_PIC, Utils.parseString(info.getRoomPic(), ""));
			userData.put(RedisConfig.USER_MSG, Utils.parseString(info.getUserMsg(), ""));
			userData.put(RedisConfig.USER_GIFT_NUM, Utils.parseString(info.getGiftNum(), "0"));
			userData.put(RedisConfig.USER_SYSTEM_ID, Utils.parseString(info.getUserSystemId(), "0"));
			userData.put(RedisConfig.USER_ACCOUNT_TYPE, Utils.parseString(info.getAccountType(), "0"));
			userData.put(RedisConfig.USER_UPDATED_TIME,
					info.getUpdatedTime() == null ? "0" : DateUtil.dateFormat(info.getUpdatedTime()));

			redis.hmset(RedisConfig.USER_INFO_PREF + userId, userData);
		}

		UserInfo user = new UserInfo();
		user.setGiftNum(Utils.parseInt(userData.get(RedisConfig.USER_GIFT_NUM)));
		user.setUserSystemId(Utils.parseInt(userData.get(RedisConfig.USER_SYSTEM_ID)));
		user.setUserMsg(Utils.parseString(userData.get(RedisConfig.USER_MSG), ""));
		user.setProfilePic(Utils.parseString(userData.get(RedisConfig.USER_PROFILE_PIC), ""));
		user.setUserId(Utils.parseLong(userData.get(RedisConfig.USER_ID)));
		user.setUserAlias(Utils.parseString(userData.get(RedisConfig.USER_ALIAS), ""));
		user.setAccountType((byte) Utils.parseInt(userData.get(RedisConfig.USER_ACCOUNT_TYPE)));
		user.setRoomPic(Utils.parseString(userData.get(RedisConfig.USER_ROOM_PIC), ""));
		user.setUserTypeId(Utils.parseInt(userData.get(RedisConfig.USER_TYPE_ID)));
		user.setVjTypeId((byte) Utils.parseInt(userData.get(RedisConfig.USER_VJ_TYPE_ID)));
		user.setVjSubtypeId((byte) Utils.parseInt(userData.get(RedisConfig.USER_VJ_SUBTYPE_ID)));
		user.setUserPic(Utils.parseString(userData.get(RedisConfig.USER_PIC), ""));
		user.setVipExpiration(null);
		long time = Utils.parseLong(userData.get(RedisConfig.USER_VIP_EXPIRATION));

		if (time > 0) {
			user.setVipExpiration(new Date(time * 1000L));
		}
		return user;
	}

	/**
	 * 获取用户id
	 * 
	 * @param loginToken
	 * @return
	 */
	public long getUserId(String loginToken) {
		if (StringUtils.isBlank(loginToken)) {
			return 0L;
		}
		return redis.getUserIdByLoginToken("SESSID:" + loginToken);
	}
}
